package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.SysRoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @PutMapping
    @ApiOperation("新增角色")
    public ResponseResult createRole(@RequestBody RoleDto roleDto) {
        sysRoleService.createRole(roleDto);
        return ResponseResult.success();
    }

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询角色列表")
    public ResponseResult<PageResponse<RoleVo>> getRoleByPage(
            @ApiParam(value = "角色名称",required = false)
            String roleName,
            @ApiParam(value = "排序")
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(value = "每页显示数量")
            @PathVariable("pageSize") Integer pageSize) {
        PageResponse<RoleVo> rolePageInfo = sysRoleService.getRoleByPage(roleName, pageNum, pageSize);
        return ResponseResult.success(rolePageInfo);
    }

    @PatchMapping
    @ApiOperation("修改角色")
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        sysRoleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation("删除角色")
    public ResponseResult deleteRole(@PathVariable("roleIds")  Long[] roleIds) {
        sysRoleService.deleteRole(roleIds);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色id查询已选中的资源")
    public ResponseResult findCheckedResources(@PathVariable("roleId") Long roleId) {
        List<String> resources = sysRoleService.getResourcesbyRoleId(roleId);
        return ResponseResult.success(resources);
    }

    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult initRoles() {
        List<RoleVo> roleList = sysRoleService.initRoles();
        return ResponseResult.success(roleList);
    }

}
